home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 112 / EnigmaAmiga112CD.iso / dalla rivista / awnpipe / awnp / awnp-docs / tutorials / tutorial2.c < prev    next >
C/C++ Source or Header  |  2000-01-01  |  8KB  |  303 lines

  1. #include <dos/dos.h>
  2. #include <clib/dos_protos.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. /*We pass 'GP' alot but it allows for multiple GUI's in a single aplication*/
  8.  int main( int argc, char *argv[]);
  9.  int getline(struct GUIpipe * GP);
  10.  __stdargs int topipe(struct GUIpipe * GP, UBYTE * data,...);
  11.  int buildgui(struct GUIpipe * GP);
  12.  int gperror(struct GUIpipe * GP,int error);
  13.  int getevent(struct GUIpipe * GP);
  14.  int gadgets(struct GUIpipe * GP);
  15.  int menu(struct GUIpipe * GP);
  16.  UBYTE * eventstr(struct GUIpipe * GP,int num);
  17.  VOID setdefaults(VOID);
  18.  
  19. /* This structure is used to magage a GUI*/
  20. /*events information and results from topipe() are kept seperate */
  21.  struct GUIpipe
  22.   {
  23.    BPTR file;
  24.    UBYTE * nextline;
  25.    int count,error;
  26.    int val1,val2,val3,val4,val5,val6;
  27.    UBYTE buf[500],str3[200];
  28.    UBYTE str1[20],result1[50],result2[50];
  29.   };
  30.  
  31. /* the structure to mantain the GUI */
  32.  struct GUIpipe myGP;
  33. /* the gadget ID's */
  34.  int namegad,agegad,sexgad,knogad,basgad,aregad,cgad,asmgad,resgad,
  35.  dongad,cangad;
  36.  
  37. /* the forms information */
  38.  int age,sex,knowledge,basic,arexx,c,asm;
  39.  UBYTE name[104];
  40.  
  41.  int main( int argc, char *argv[] )
  42.   {
  43.    struct GUIpipe * GP = &myGP;
  44.    int stop=0;
  45.    setdefaults();
  46. /* build the GUI and use it*/
  47.    if(!buildgui(GP))
  48.     {
  49. /* we loop until we are told to stop or an error happens*/
  50.      while(!GP->error&&!stop)
  51.       {
  52. /* read an event.*/
  53.        getevent(GP);
  54. /* the first two letter of each event type are different. For this GUI one
  55. letter would be enough . SASC requires you turn the Multiple Character
  56. Constants option on (MCConstants) .*/
  57.        switch(GP->str1[1]+(GP->str1[0]<<8))
  58.         {
  59.          case('ga'):
  60.          stop=gadgets(GP);
  61.          break;
  62.          case('me'):
  63.          stop=menu(GP);
  64.          break;
  65.          case('cl'):
  66.          printf("Use Closed Window or uses CTRl\\\n");
  67.          stop=1;
  68.          break;
  69.         }
  70.       }
  71.     }
  72.  
  73. /*close the pipe connection*/
  74.    if(GP->file)Close(GP->file);
  75.    exit( 0 );
  76.   }
  77.  
  78. /* read a  line from the pipe*/
  79. /* get rid of the old line if there is one*/
  80.  int getline(struct GUIpipe * GP)
  81.   {
  82.    int in;
  83.    if(GP->nextline)
  84.     {
  85.      GP->count-=(GP->nextline-GP->buf);
  86.      memmove(GP->buf,GP->nextline,GP->count);
  87.      GP->nextline=0;
  88.     }
  89. /*read pipe until we have an linefeed*/
  90.    while (!(GP->nextline=memchr(GP->buf,'\n',GP->count)))
  91.     {
  92.  
  93.      if(!(in=Read(GP->file,&(GP->buf[GP->count]),499-GP->count)))
  94.       {
  95. /*error on EOF or lines over 499 chars.*/
  96.        gperror(GP,1);
  97.        return(1);
  98.       }
  99.      GP->count+=in;
  100.     }
  101.  
  102. /* mark the end of line*/
  103.    *GP->nextline++=0;
  104.    return(0);
  105.   }
  106.  
  107. /*write data to the pipe, read the responce from the pipe and parse it,
  108.  return the value of the second parameter is the responce is ok,
  109.  return 0 if we have a problem*/
  110.  __stdargs int topipe(struct GUIpipe * GP, UBYTE * data,...)
  111.   {
  112.    VFPrintf(GP->file,data,(APTR)(4+(int)(&data)) );
  113.    if(getline(GP)) return(0);
  114.    sscanf(GP->buf,"%50s %50s",GP->result1,GP->result2);
  115.    if(!strcmp(GP->result1,"ok"))return(atoi(GP->result2));
  116.    gperror(GP,2);
  117.    return(0);
  118.   }
  119.  
  120.  
  121. /* GP->error could simply be set rather then calling this routine
  122. but its politer to alert the user somehow */
  123.  int gperror(struct GUIpipe * GP,int error)
  124.   {
  125.    GP->error=error;
  126.    printf("ERROR %ld\n",GP->error);
  127.    return(0);
  128.   }
  129.  
  130. /*open the file on AWNPipe:, send the window and gadget definitions,
  131.  open the GUI window, return 0 for sucess or an error number*/
  132.  buildgui(struct GUIpipe * GP)
  133.   {
  134. /* Open pipe 'tut2' with GUI creation option '/xc' */
  135.    if(!( GP->file=Open("awnpipe:tut2/xc",MODE_OLDFILE))) return(1);
  136.  
  137.    /* define the window */
  138.  
  139.    /* The first line oF every GUI is the window definition. The window is titled
  140. "Tutorial 2" and its elements will be laid out verticaly (v). It has a
  141. closegadget (cg) , depthgadget (dg) , and dragbar (db). It will have spaces
  142. inbetween its gadgets (si). It will open on the topleft (tl) of the screen
  143. becoming active (a) when opened. */
  144.  
  145.    topipe(GP," \"Tutorial 2\" v cg dg db si a tl\n");
  146.  
  147.    /* define the gadgets*/
  148.  
  149.    topipe(GP," layout b 0 v\n");
  150.  
  151.    /* Labels are used to tell the user what information to enter in each gadget.
  152. These labels are unatached (ua) when they are defined so don't go directly
  153. into the GUI. Instead they are attached to the following gadget by the
  154. childlabel (chl) keyword. */
  155.  
  156.    topipe(GP," label gt \"Name: \" ua\n");
  157.    namegad=topipe(GP,"string lj chl\n");
  158.    topipe(GP," label gt \"Age: \" ua\n");
  159.    agegad=topipe(GP,"integer chl minn 5 maxn 115 arrows defn 30  weiw 0\n");
  160.    topipe(GP," label gt \"Sex: \" ua\n");
  161.    sexgad=topipe(GP,"radiobutton rl \"Male|Female\" chl\n");
  162.    topipe(GP," label gt \"Knowledge: \" ua\n");
  163.    knogad=topipe(GP,"chooser pu cl \"Novice|Average|Good|Expert\" chl\n");
  164.    topipe(GP," label gt \"Language(s): \" ua\n");
  165.    topipe(GP," layout b 0 chl\n");
  166.    basgad=topipe(GP,"checkbox gt \"Basic \" chl\n");
  167.    aregad=topipe(GP,"checkbox gt \"Arexx \" chl\n");
  168.    cgad=topipe(GP,"checkbox gt \"C \" chl\n");
  169.    asmgad=topipe(GP,"checkbox gt \"ASM \" chl\n");
  170.    topipe(GP," le\n");
  171.    topipe(GP," le\n");
  172.    topipe(GP," layout si so\n");
  173.    resgad= topipe(GP,"button gt \"Reset Form\" c\n");
  174.    dongad= topipe(GP,"button gt \"Done\" c\n");
  175.    cangad= topipe(GP,"button gt \"Cancel\" c\n");
  176.    topipe(GP," le\n");
  177.    topipe(GP," menu gt \"Project  |About|$!   Tutorial 2   |$!  AWNPipe: Example\"\n");
  178.    topipe(GP," menu gt \"Data|@AShow all data|Show part|$@PPersonal|$@SSkill\"\n");
  179.  
  180.    /*open the GUI window if all is ok*/
  181.    if(!GP->error) topipe(GP,"open\n");
  182.    return(GP->error);
  183.   }
  184.  
  185. /* read a line form the pipe and parse it for event information*/
  186.  int getevent(struct GUIpipe * GP)
  187.   {
  188.    UBYTE * s3;
  189.    if(!getline(GP))
  190.     {
  191.      sscanf(GP->buf,"%20s %d %d %d %d %d",
  192.             GP->str1,&GP->val2,&GP->val3,
  193.             &GP->val4,&GP->val5,&GP->val6);
  194.      sscanf(GP->buf,"%d",&GP->val1);
  195.      if (s3=eventstr(GP,3))
  196.       {
  197.        if (strlen(s3)<200)strcpy(GP->str3,s3);
  198.        else memmove(GP->str3,s3,199);
  199.       }
  200.     }
  201.    return(GP->error);
  202.   }
  203.  
  204. /* find the start of the 'num' parameter.
  205. ONLY CALL THIS FUCNTION IMEDIATLY AFTER RECEIVING A LINE.
  206. Then store a copy of the string, NOT THE POINTER */
  207.  UBYTE * eventstr(struct GUIpipe * GP,int num)
  208.   {
  209.    UBYTE *a;
  210.    a=GP->buf;
  211.    while((num--)>1)
  212.     {
  213.      a=strchr(a,' ');
  214.      if(!a)return(0);
  215.      a++;
  216.     }
  217.    return(a);
  218.   }
  219.  
  220. /* store the information from the event or perform an action */
  221. /* we return 1 if the gui should be closed, or 0 to keep going*/
  222.  int gadgets(struct GUIpipe * GP)
  223.   {
  224.    int a;
  225.    a=GP->val2;
  226.    if(a==agegad)     age=GP->val3;
  227.    if(a==sexgad)     sex=GP->val3;
  228.    if(a==knogad)     knowledge=GP->val3;
  229.    if(a==basgad)     basic=GP->val3;
  230.    if(a==aregad)     arexx=GP->val3;
  231.    if(a==cgad)       c=GP->val3;
  232.    if(a==asmgad)     asm=GP->val3;
  233.    if(a==namegad)    strcpy(name,GP->str3);
  234.    if(a==resgad)
  235.     {
  236. /* close the GUI, clean the GUIpipe structure */
  237.      Close(GP->file);
  238.      GP->file=0;
  239.      GP->nextline=0;
  240.      GP->count=0;
  241.      GP->buf[0]=0;
  242. /* try to make a new GUI*/
  243.      setdefaults();
  244.      if(buildgui(GP))return(1);
  245.     }
  246.    if(a==cangad)
  247.     {
  248.      printf("User Canceled\n");
  249.      return(1);
  250.     }
  251.    if(a==dongad)
  252.     {
  253.      printf("name: %s\n age: %ld\n sex: %ld\n", name,age,sex);
  254.      printf("knowledge: %ld\n basic: %ld arexx: %ld c: %ld asm: %ld \n",
  255.             knowledge,basic,arexx,c,asm);
  256.      return(1);
  257.     }
  258.    return(0);
  259.   }
  260.  
  261. /*react to the menu event. menu#=val2, menuitem#=val3,subitem#=val4
  262.  menu 0 is only informational so we ignore it and just handle menu 1*/
  263.  int menu(struct GUIpipe * GP)
  264.   {
  265.    if(GP->val2==1)
  266.     {
  267.      if(GP->val3==0)
  268.       {
  269.        printf("name %s age %ld sex %ld\n", name,age,sex);
  270.        printf("knowledge %ld basic %ld arexx %ld c %ld asm %ld \n",
  271.               knowledge,basic,arexx,c,asm);
  272.       }
  273.  
  274.      if(GP->val3==1)
  275.       {
  276.        if(GP->val4==0)
  277.         {
  278.          printf("name %s age %ld sex %ld\n", name,age,sex);
  279.         }
  280.        if(GP->val4==1)
  281.         {
  282.          printf("knowledge %ld basic %ld arexx %ld c %ld asm %ld \n",
  283.                 knowledge,basic,arexx,c,asm);
  284.         }
  285.       }
  286.     }
  287.    return(0);
  288.   }
  289.  
  290. /*initialize our information to default state*/
  291.  VOID setdefaults()
  292.   {
  293.    *name=0;
  294.    age=30;
  295.    sex=0 ;
  296.    knowledge=0;
  297.    basic=0;
  298.    c=0;
  299.    asm=0;
  300.    arexx=0;
  301.   }
  302.  
  303.